package com.example.mychat.controller;

import com.example.mychat.entity.User;
import com.example.mychat.service.impl.UserServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/user")
@Tag(name = "用户信息接口")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    //从数据库查询获得json数据
    @GetMapping("/queryAll")
    @Operation(summary = "查询全部用户信息")
    public List<User> queryAll(){
        return userService.findAllUser();
    }

    @GetMapping("/insert")
    @Operation(summary = "新增用户")
    public Boolean insert(@Parameter(name = "username", description = "账号")String username,
                          @Parameter(name = "password", description = "密码")String password,
                          @Parameter(name = "image", description = "头像", required = false)int image,
                          @Parameter(name = "telephone", description = "手机号", required = false)String telephone,
                          @Parameter(name = "email", description = "电子邮件", required = false)String email){
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            return false;
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setImage(image);
        user.setTelephone(telephone);
        user.setEmail(email);
        return userService.insertUser(user) > 0;
    }

    @GetMapping("/update")
    @Operation(summary = "更新用户")
    public Boolean update(@Parameter(name = "id", description = "账号ID")Integer id,
                          @Parameter(name = "username", description = "账号")String username,
                          @Parameter(name = "password", description = "密码")String password,
                          @Parameter(name = "image", description = "头像", required = false)int image,
                          @Parameter(name = "telephone", description = "手机号", required = false)String telephone,
                          @Parameter(name = "email", description = "电子邮件", required = false)String email){
        if(id==null || id<1 || StringUtils.isEmpty(username) || StringUtils.isEmpty(password) ){
            return false;
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setImage(image);
        user.setTelephone(telephone);
        user.setEmail(email);
        return userService.updUser(user) > 0;
    }

    @GetMapping("/delete")
    @Operation(summary = "删除用户")
    public Boolean delete(@Parameter(name = "id", description = "账号ID")Integer id){
        if(id==null || id<1){
            return false;
        }
        return userService.delUser(id) > 0;
    }
}
